package tests;

import oracle.kv.KVStore;
import oracle.kv.KVStoreConfig;
import oracle.kv.KVStoreFactory;
import oracle.kv.Key;
import oracle.kv.Value;
import oracle.kv.ValueVersion;

import java.util.ArrayList;

/**
 * Big Data @ FHV 2012
 * 
 * Test operations for the Oracle NoSql database
 * Possible tests: insert a key value pair and read the phone numer
 * 
 * @author Michael Sieber / Philipp Rusch 
 *
 */
public class OraNoSqlCRUD {
	
	//do not forget to change the DNS name!
	String[] _hhosts = {"ec2-54-246-157-129.eu-west-1.compute.amazonaws.com:5000"}; 
	KVStoreConfig _kconfig;
	KVStore _kvstore; 	
	ArrayList<String> _majorComponents = new ArrayList<String>();
	ArrayList<String> _minorComponents = new ArrayList<String>();
	
	/**
	 * initialize connection to amazon web services
	 * note: please enter bigorasrv1 into your local hosts file with
	 * the elastic ip address
	 */
	public void initConnection(){
		_kconfig = new KVStoreConfig("BigData", _hhosts);
		_kvstore = KVStoreFactory.getStore(_kconfig);
	}
	
	/**
	 * Reads the dummy record
	 */
	public void readRecord(){
		initConnection();
		
		// Define the major and minor path components for the key
		_majorComponents.add("Smith");
		_majorComponents.add("Bob");

		_minorComponents.add("phonenumber");

		// Create the key
		Key myKey = Key.createKey(_majorComponents, _minorComponents);

		// Now retrieve the record. Note that we do not show the creation of 
		// the kvstore handle here.

		ValueVersion vv = _kvstore.get(myKey);
		Value v = vv.getValue(); 
		String data = new String(v.getValue());
		System.out.println("phone number is: " + data);
	}
	
	/**
	 * Create a dummy record
	 */
	public void createRecord(){
		initConnection();
		
		// Define the major and minor path components for the key
		_majorComponents.add("Smith");
		_majorComponents.add("Bob");
	
		_minorComponents.add("phonenumber");

		// Create the key
		Key myKey = Key.createKey(_majorComponents, _minorComponents);

		String data = "408 555 5555";

		// Create the value. Notice that we serialize the contents of the
		// String object when we create the value.
		Value myValue = Value.createValue(data.getBytes());

		// Now put the record. Note that we do not show the creation of the
		// kvstore handle here.

		_kvstore.put(myKey, myValue); 
	}
	public static void main(String[] args){
		OraNoSqlCRUD test = new OraNoSqlCRUD();
		test.readRecord();
		
	}
}
